import { Model } from 'dva';
import { demoGetList, IDemoGetListData } from './service';

export interface IDemoPageListItem {
  foo: string;
  bar: string;
}

export interface IDemoPageState {
  // skip: number
  // limit: number
  // count: number
  test: string;
  list: IDemoPageListItem[];
}

const initialState: IDemoPageState = {
  // skip: 0,
  // limit: 0,
  // count: 0,
  test: '这是自定义数据',
  list: [],
};

const indexModel: Model = {
  namespace: 'index',
  state: initialState,
  reducers: {
    _demoGetList(state, { payload: list }) {
      return { ...state, list };
    },
  },
  effects: {
    *demoGetList(action, { put, call }) {
      // const data: IDemoGetListData = action.payload || {};
      const data = yield call(demoGetList, {});

      yield put({ type: '_demoGetList', payload: data.data });
      return data;
    },
  },
  // subscriptions: {
  //   setup({ history, dispatch }: SubscriptionAPI, done: () => void) {
  //     return history.listen(({ pathname }) => {
  //       if (pathname === '/index') {
  //         dispatch({ type: 'demoGetList' });
  //       }
  //     });
  //   },
  // },
};

export default indexModel;
